<?php
  include("header.php");
  include("functions.php");
  
  if ($_SESSION['user_level'] != 1) {
      header("location: redirect.php");
  } else {
      $pageTitle = "Permissions";
	  
      $guest_access = get_value_of('$guest_access');
      $guest_access = trim($guest_access);
	  $guest_access = preg_replace("#[^0-9]#", "", $guest_access);
	  
      $topics_for_guests = get_value_of('$topics_for_guests');
      $topics_for_guests = trim($topics_for_guests);
      $topics_for_guests = preg_replace("#[^0-9]#", "", $topics_for_guests);
	  
	  
      if ($guest_access == "1") {$guest_y = "checked";$guest_n = "";} else {
          $guest_y = "";
          $guest_n = "checked";
      }
          if ($topics_for_guests == "1") {$topic_y = "checked";$topic_n = "";} else {
          $topic_y = "";
          $topic_n = "checked";
      }
	  
	  $allow_guest_posts = get_value_of('$allow_guest_posts');
      $allow_guest_posts = trim($allow_guest_posts);
      $allow_guest_posts = preg_replace("#[^0-9]#", "", $allow_guest_posts);
	      if ($allow_guest_posts == "1") {$agpy = "checked";$agpn = "";} else {
          $agpy = "";
          $agpn = "checked";
      }
	  
      $captcha_for_guests = get_value_of('$captcha_for_guests');
      $captcha_for_guests = trim($captcha_for_guests);
      $captcha_for_guests = preg_replace("#[^0-9]#", "", $captcha_for_guests);
	      if ($captcha_for_guests == "1") {$cay = "checked";$can = "";} else {
          $cay = "";
          $can = "checked";
      }
	  $allow_avatars = get_value_of('$allow_avatars');
      $allow_avatars = trim($allow_avatars);
      $allow_avatars = preg_replace("#[^0-9]#", "", $allow_avatars);
	      if ($allow_avatars == "1") {$avy = "checked";$avn = "";} else {
          $avy = "";
          $avn = "checked";
      }
	  $avatar_width = get_value_of('$avatar_width');
      $avatar_width = trim($avatar_width);
	  $avatar_height = get_value_of('$avatar_height');
      $avatar_height = trim($avatar_height);
	  
	  $avatars_upload = get_value_of('$avatars_upload');
      $avatars_upload = trim($avatars_upload);
      $avatars_upload = preg_replace("#[^0-9]#", "", $avatars_upload);
	      if ($avatars_upload == "1") {$avupy = "checked";$avupn = "";} else {
          $avupy = "";
          $avupn = "checked";
      }
	  $avatars_upload_size = get_value_of('$avatars_upload_size');
      $avatars_upload_size = trim($avatars_upload_size);
	  $avatars_upload_size = preg_replace("#[^0-9]#", "", $avatars_upload_size);
	  
	  $allow_user_signature = get_value_of('$allow_user_signature');
      $allow_user_signature = trim($allow_user_signature);
	  $allow_user_signature = preg_replace("#[^0-9]#", "", $allow_user_signature);
	  	      if ($allow_user_signature == "1") {$sigy = "checked";$sign = "";} else {
          $sigy = "";
          $sign = "checked";
      }
	  $signature_max_char = get_value_of('$signature_max_char');
      $signature_max_char = trim($signature_max_char);
	  $signature_max_char = preg_replace("#[^0-9]#", "", $signature_max_char);
	  
	  $images_in_signature = get_value_of('$images_in_signature');
      $images_in_signature = trim($images_in_signature);
	  $images_in_signature = preg_replace("#[^0-9]#", "", $images_in_signature);
	  	      if ($images_in_signature == "1") {$imgsy = "checked";$imgsn = "";} else {
          $imgsy = "";
          $imgsn = "checked";
      }
	  $links_in_signature = get_value_of('$links_in_signature');
      $links_in_signature = trim($links_in_signature);
	  $links_in_signature = preg_replace("#[^0-9]#", "", $links_in_signature);
	  	      if ($links_in_signature == "1") {$linky = "checked";$linkn = "";} else {
          $linky = "";
          $linkn = "checked";
      }
	  
	  $guest_view_profile = get_value_of('$guest_view_profile');
      $guest_view_profile = trim($guest_view_profile);
	  $guest_view_profile = preg_replace("#[^0-9]#", "", $guest_view_profile);
	  	      if ($guest_view_profile == "1") {$gvy = "checked";$gvn = "";} else {
          $gvy = "";
          $gvn = "checked";
      }
	  $guest_show_links = get_value_of('$guest_show_links');
      $guest_show_links = trim($guest_show_links);
	  $guest_show_links = preg_replace("#[^0-9]#", "", $guest_show_links);
	  	      if ($guest_show_links == "1") {$gly = "checked";$gln = "";} else {
          $gly = "";
          $gln = "checked";
      }
	  
	  $guest_search = get_value_of('$guest_search');
      $guest_search = trim($guest_search);
	  $guest_search = preg_replace("#[^0-9]#", "", $guest_search);
	  	      if ($guest_search == "1") {$gsy = "checked";$gsn = "";} else {
          $gsy = "";
          $gsn = "checked";
      }
	  
	  $post_approve = get_value_of('$post_approve');
      $post_approve = trim($post_approve);
	  $post_approve = preg_replace("#[^0-9]#", "", $post_approve);
	  	      if ($post_approve == "1") {$pay = "checked";$pan = "";} else {
          $pay = "";
          $pan = "checked";
      }
	  
	  $auto_approve_after_posts = get_value_of('$auto_approve_after_posts');
      $auto_approve_after_posts = trim($auto_approve_after_posts);
	  $auto_approve_after_posts = preg_replace("#[^0-9]#", "", $auto_approve_after_posts);
	  
	  
      if ($_SERVER['REQUEST_METHOD'] != 'POST') {
          $tags = array('{SIGNATURE_TITLE}','{AVATARS_TITLE}','{TITLE}','{CONFIRM}','{GUEST}', '{GUEST_Y}', '{GUEST_N}', '{TOPIC}', '{TOPIC_Y}', '{TOPIC_N}','{GUEST_POST}','{GP_Y}','{GP_N}','{CAPTCHA_G}','{CA_Y}','{CA_N}','{AVATARS}','{AV_Y}','{AV_N}','{AVATARS_W}','{AVATARS_W_VALUE}','{AVATARS_H_VALUE}','{AVATAR_UPLOAD}','{AVU_Y}','{AVU_N}','{AVATAR_UP_SIZE}','{AVATAR_UP_VALUE}','{U_SIGNATURE}','{U_SIGY}','{U_SIGN}','{SIG_CHAR}','{SIG_CHAR_VALUE}','{SIG_IMG}','{SI_Y}','{SI_N}','{SIG_LINKS}','{SIL_Y}','{SIL_N}','{GUEST_VIEW_PROFILES}','{GVY}','{GVN}','{GUEST_LINKS}','{GLY}','GLN','{GUEST_SEARCH}','{GSY}','{GSN}','{POST_APPROVAL_TITLE}','{POST_APPROVAL}','{AP_Y}','{AP_N}','{APP_POSTS}','{APPROVAL_VALUE}','{POSTS}');
		  
          $data = array('Signature','Avatars','Guests','Confirm','Allow guests to access forum', $guest_y, $guest_n, 'Allow guest to read topics', $topic_y,$topic_n,'Allow guest to post reply',$agpy,$agpn,'Captcha for guests',$cay,$can,'Allow avatars',$avy,$avn,'Avatar size (px)',$avatar_width,$avatar_height,'Avatar uploads',$avupy,$avupn,'Avatar upload size (kb)',$avatars_upload_size,'Allow user signature',$sigy,$sign,'Signature max. characters',$signature_max_char,'Allow images in signature',$imgsy,$imgsn,'Allow links in signature',$linky,$linkn,'Guests can view user profiles',$gvy,$gvn,'Guest can see links in posts',$gly,$gln,'Guest can search forum',$gsy,$gsn,'Posts','Post approval',$pay,$pan,'Auto approve after<br>(0 - always unnaproved)',$auto_approve_after_posts,'posts');

          echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/permission.html"));
      } else {
	  $value = $_POST['approve_value'];
	  if ($_POST['approve_value'] <=0) {$value = 0;}
          write_value_of('$guest_access', "$guest_access", $_POST['guest_access']);
          write_value_of('$topics_for_guests', "$topics_for_guests", $_POST['guest_read_t']);
		  write_value_of('$allow_guest_posts', "$allow_guest_posts", $_POST['guest_post']);
		  write_value_of('$captcha_for_guests', "$captcha_for_guests", $_POST['guest_captcha']);
		  write_value_of('$allow_avatars', "$allow_avatars", $_POST['avatars']);
		  write_value_of('$avatar_width', "$avatar_width", $_POST['avatar_w']);
		  write_value_of('$avatar_height', "$avatar_height", $_POST['avatar_h']);
		  write_value_of('$avatars_upload', "$avatars_upload", $_POST['avatars_upload']);
		  write_value_of('$avatars_upload_size', "$avatars_upload_size", $_POST['avatar_up_size']);
		  write_value_of('$allow_user_signature', "$allow_user_signature", $_POST['signature']);
		  write_value_of('$signature_max_char', "$signature_max_char", $_POST['sig_char']);
		  write_value_of('$images_in_signature', "$images_in_signature", $_POST['sig_img']);
		  write_value_of('$links_in_signature', "$links_in_signature", $_POST['sig_link']);
		  write_value_of('$guest_view_profile', "$guest_view_profile", $_POST['guest_vp']);
		  write_value_of('$guest_show_links', "$guest_show_links", $_POST['guest_vlink']);
		  write_value_of('$guest_search', "$guest_search", $_POST['guest_search']);
		  write_value_of('$post_approve', "$post_approve", $_POST['post_approval']);
		  write_value_of('$auto_approve_after_posts', "$auto_approve_after_posts", $value);
		  
          echo "<b>Information</b><hr><br /><div align='center'>Configuration updated successfully.<br /><br /><a href = 'permission.php'>Back to previous page</a></div>";
      }
      // Get all the page's HTML into a string
      $pageContents = ob_get_contents();
      // Wipe the buffer 
      ob_end_clean();
      echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
  }
?>
